package org.dxj.restaurantmanagementsystem.dao.impl;

import org.dxj.restaurantmanagementsystem.dao.IUserDao;
import org.dxj.restaurantmanagementsystem.entity.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ding
 * @date 2019/11/23 17:52
 */
public class UserDaoImpl implements IUserDao {

    /**
     * 增加用户信息UserDao
     * @param user
     * @return boolean
     * @throws IOException
     */
    @Override
    public boolean insertUserDao(User user) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("RestaurantManagementSystem\\User.txt",true));
        String returnString=String.format("%s,%s,%s,%s,%s,%s",user.getId(),user.getUserName(),user.getPassword(),user.getQuestion(),user.getAnswer(),user.getLevel());
        bufferedWriter.write(returnString);
        bufferedWriter.newLine();
        bufferedWriter.close();
        return true;
    }

    /**
     * 根据用户id删除用户UserDao
     * @param id
     * @return boolean
     */
    @Override
    public boolean deleteUserDao(Integer id) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("RestaurantManagementSystem\\User.txt"));
        List<User> userList=new ArrayList<>();
        String string;
//        System.out.println("-----");
        while ((string=bufferedReader.readLine())!=null){
            String[] dates=string.split(",");
            User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
            System.out.println(user);
            userList.add(user);
        }
        for (int i = 0; i <=userList.size()-1 ; i++) {
          User user=userList.get(i);
          if (user.getId().equals(id)){
              userList.remove(i);
          }
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("RestaurantManagementSystem\\User.txt"));
        for (int i = 0; i <userList.size() ; i++) {
            User user=userList.get(i);
            String returnString=String.format("%s,%s,%s,%s,%s,%s",user.getId(),user.getUserName(),user.getPassword(),user.getQuestion(),user.getAnswer(),user.getLevel());
            bufferedWriter.write(returnString);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
        return false;
    }

    /**
     * 根据用户id更改用户信息UserDao
     * @param user
     * @param id
     * @return boolean
     */
    @Override
    public boolean updateUserDao(User user, Integer id) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("RestaurantManagementSystem\\User.txt"));
        List<User> userList=new ArrayList<>();
        String string;
//        System.out.println("-----");
        while ((string=bufferedReader.readLine())!=null){
            String[] dates=string.split(",");
            User returnUser=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
            System.out.println(returnUser);
            userList.add(returnUser);
        }
        for (int i = 0; i <=userList.size()-1 ; i++) {
            User returnUser=userList.get(i);
            if (returnUser.getId().equals(id)){
                userList.set(i,user);
            }
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("RestaurantManagementSystem\\User.txt"));
        for (int i = 0; i <userList.size() ; i++) {
            User returnUser=userList.get(i);
            String returnString=String.format("%s,%s,%s,%s,%s,%s",returnUser.getId(),returnUser.getUserName(),returnUser.getPassword(),returnUser.getQuestion(),returnUser.getAnswer(),returnUser.getLevel());
            bufferedWriter.write(returnString);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
        return false;
    }

    /**
     * 根据用户id查询用户信息UserDao
     * @param id
     * @return List<User>
     * @throws IOException
     */
    @Override
    public List<User> selectUserByIdDao(Integer id) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("RestaurantManagementSystem\\User.txt"));
        String string;
        List<User> userList=new ArrayList<>();
        while((string=bufferedReader.readLine())!=null){
            String[] dates=string.split(",");
            if (Integer.parseInt(dates[0])==id){
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
     }
        bufferedReader.close();
        return userList;
    }

    /**
     * 查询用户所有信息UserDao
     * @return List<User>
     * @throws IOException
     */
    @Override
    public List<User> selectUserDao() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("RestaurantManagementSystem\\User.txt"));
        String string;
        List<User> userList=new ArrayList<>();
        while((string=bufferedReader.readLine())!=null){
            String[] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
        }
        bufferedReader.close();
        return userList;
    }
}
